#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
struct stu
{
	int time;
	int prince;
};
int main()
{
	struct stu s[20];
	int T = 0; int k = 0; int x = 0;
	scanf("%d", &T); int i, j;
	for (x = 0; x < T; x++)
	{
		scanf("%d", &k);
		for (j = 0; j < k; j++)
		{
			scanf("%d%d", &s[j].time, &s[j].prince);
		}
		for (j = 1; j < k; j++)
		{
			for (i = 0; i < k - 1; i++)
			{
				if (s[i].time > s[i + 1].time)
				{
					int tmp = 0;
					tmp = s[i + 1].time;
					s[i + 1].time = s[i].time;
					s[i].time = tmp;
					tmp = s[i + 1].prince;
					s[i + 1].prince = s[i].prince;
					s[i].prince = tmp;
				}
			}
		}
		for (i = 0; i < k; i++)
		{
			printf("%d\n", s[i].prince);
		}
	}
	return  0;
}